package com.yourname.bank_sim;

import com.yourname.bank_sim.entity.Routes;
import com.yourname.bank_sim.entity.Station;
import com.yourname.bank_sim.list.CustomList;

import java.io.*;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

public class DataStore {
    private static CustomList<Station> stations=new CustomList<>();
    private static CustomList<Routes> routes=new CustomList<>();
    public static  void addStation(Station station){
        stations.add(station);
    }
    public static  void addRoute(Routes route){
        routes.add(route);
    }
    public static void updateStation(Station newStation){
        stations.updateById(newStation.getId(),newStation);
    }
    public static void updateRoute(Routes route){
        routes.updateById(route.getId(),route);
    }
    public static void deleteStation(String id){
        stations.deleteById(id,new Station());
    }
    public static void deleteRoute(String id){
        routes.deleteById(id,new Routes());
    }
    private static void saveStations(){
        File f = new File("station.txt");
        try {
            if (!f.exists()) {
                f.createNewFile();
            }
            StringBuilder printStr = new StringBuilder();
            printStr.append("编号,站点名,收费区编号\n");
            for (int i = 0; i < stations.size(); i++) {
                Station station = stations.get(i);
                printStr.append(station.getId()).append(",").append(station.getName()).append(",")
                        .append(station.getArea()).append("\n");
            }
            FileOutputStream fileOutputStream = new FileOutputStream(f);
            fileOutputStream.write(printStr.toString().getBytes(StandardCharsets.UTF_8));
            fileOutputStream.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    private static void loadStations(){
        File f = new File("station.txt");
        try {
            if (!f.exists()) {
                f.createNewFile();
            }
            BufferedReader fileReader=new BufferedReader(new FileReader(f));
            fileReader.readLine();
            while (true){
                String line = fileReader.readLine();
                if (line==null)
                    break;
                String[] split = line.split(",");
                Station station=new Station();
                station.setId(split[0]);
                station.setName(split[1]);
                station.setArea(Integer.parseInt(split[2]));
                stations.add(station);
            }
            fileReader.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    private static void saveRoutes(){
        File f = new File("route.txt");
        try {
            if (!f.exists()) {
                f.createNewFile();
            }
            StringBuilder printStr = new StringBuilder();
            printStr.append("编号-线路名-途径车站编号\n");
            for (int i = 0; i < routes.size(); i++) {
                Routes routes1 = routes.get(i);
                StringBuilder stationBuilder=new StringBuilder("[");
                String[] stations1 = routes1.getStations();
                for (int j = 0; j < stations1.length; j++) {
                    stationBuilder.append(stations1[j]);
                    if (j + 1 < stations1.length) {
                        stationBuilder.append(",");
                    }
                }
                stationBuilder.append("]");
                printStr.append(routes1.getId()).append("-").append(routes1.getName()).append("-")
                        .append(stationBuilder).append("\n");
            }
            FileOutputStream fileOutputStream = new FileOutputStream(f);
            fileOutputStream.write(printStr.toString().getBytes(StandardCharsets.UTF_8));
            fileOutputStream.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    private static void loadRoutes(){
        File f = new File("route.txt");
        try {
            if (!f.exists()) {
                f.createNewFile();
            }
            BufferedReader fileReader=new BufferedReader(new FileReader(f));
            fileReader.readLine();
            while (true){
                String line = fileReader.readLine();
                if (line==null)
                    break;
                String[] split = line.split("-");
                Routes routes1=new Routes();
                routes1.setId(split[0]);
                routes1.setName(split[1]);
                String string = split[2];
                String[] split1 = string.substring(1, string.length() - 1).split(",");
                routes1.setStations(split1);
                routes.add(routes1);
            }
            fileReader.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


    public static void saveData(){
        saveStations();
        saveRoutes();
    }
    public static void loadData(){
        loadStations();
        loadRoutes();
    }
    public static void findRoute(String start,String end){
        for (Routes r:routes){
            int stationNumber=0;
            boolean findStart=false;
            boolean findEnd=false;
            int startArea=0;
            int endArea=0;
            for (String s:r.getStations()) {
                if (s.equals(start)) {
                    findStart=true;
                    startArea=stations.getObjectById(s,new Station()).getArea();
                }
                if (findStart){
                    stationNumber++;
                }
                if (s.equals(end)){
                    findEnd=true;
                    endArea=stations.getObjectById(s,new Station()).getArea();
                }
                if (findEnd&&findStart){
                    int cost=Math.abs(startArea-endArea)+1;
                    System.out.println(String.format("""
                                找到线路：%s
                                途径车站数：%d
                                费用：%d
                                """,r.getId(),stationNumber-1,cost));
                    return;
                }
            }
            System.out.println("无法通行，线路无法到达"+end);
        }
    }
}
